package kz.kstu.scantest.business.server.impl;

import kz.kstu.scantest.business.entity.CommonEntityConstant;
import kz.kstu.scantest.business.entity.test.Test;
import kz.kstu.scantest.business.server.api.TestService;
import org.apache.log4j.Logger;

import javax.persistence.Query;
import java.util.Collection;
import java.util.List;

/**
 * @author Filipp_Stankevich
 *         created: 13.09.11 22:26
 */
public class TestServiceImpl extends AbstractService implements TestService {

    private final static Logger LOGGER = Logger.getLogger(TestServiceImpl.class);

    public void saveTests(Collection<Test> tests) {
        for (Test test : tests) {
            Query query = getEntityManager().createNamedQuery(CommonEntityConstant.FIND_TEST_BY_VARIANT);
            query.setParameter("variant", test.getVariant());
            List<Test> existsTests = query.getResultList();
            for (Test existTest : existsTests) {
                getEntityManager().remove(existTest);
            }
            getEntityManager().persist(test);
            LOGGER.info("Test was saved, variant = " + test.getVariant());
        }
    }
}
